package com.seu.project.seuquiz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seu.project.seuquiz.common.ErrorCode;
import com.seu.project.seuquiz.constant.CommonConstant;
import com.seu.project.seuquiz.exception.BusinessException;
import com.seu.project.seuquiz.mapper.UserScoreMapMapper;
import com.seu.project.seuquiz.model.dto.question.QuestionQueryRequest;
import com.seu.project.seuquiz.model.dto.userScoreMap.UserScoreMapQueryRequest;
import com.seu.project.seuquiz.model.entity.CollegeContestMap;
import com.seu.project.seuquiz.model.entity.Question;
import com.seu.project.seuquiz.model.entity.UserScoreMap;
import com.seu.project.seuquiz.service.UserScoreMapService;
import com.seu.project.seuquiz.util.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author lenovo
* @description 针对表【user_score_map(用户-成绩map表)】的数据库操作Service实现
* @createDate 2023-07-15 13:47:58
*/
@Service
public class UserScoreMapServiceImpl extends ServiceImpl<UserScoreMapMapper, UserScoreMap>
    implements UserScoreMapService {
    @Override
    public long questionAdd(String studentId, String contestId, Integer time, Integer score) {
        if (StringUtils.isAnyBlank(studentId, contestId)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "学号、竞赛id不能为空");
        }
        if (time == null || score == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "用时、得分不能为空");
        }

        synchronized (studentId + contestId) {
            UserScoreMapQueryRequest userScoreMapQueryRequest = new UserScoreMapQueryRequest();
            userScoreMapQueryRequest.setStudentId(studentId);
            userScoreMapQueryRequest.setContestId(contestId);
            int times = (int) (this.count(this.getQueryWrapper(userScoreMapQueryRequest)) + 1);

            UserScoreMap userScoreMap = new UserScoreMap();
            userScoreMap.setStudentId(studentId);
            userScoreMap.setContestId(contestId);
            userScoreMap.setTimes(times);
            userScoreMap.setTime(time);
            userScoreMap.setScore(score);

            boolean save = this.save(userScoreMap);
            if(!save){
                throw new BusinessException(ErrorCode.SYSTEM_ERROR,"添加失败，数据库错误");
            }
            return userScoreMap.getId();
        }
    }

    /**
     *
     * @param request
     * @return
     */
    @Override
    public QueryWrapper<UserScoreMap> getQueryWrapper(UserScoreMapQueryRequest request) {
        QueryWrapper<UserScoreMap> queryWrapper = new QueryWrapper<>();
        if (request == null) {
            return null;
        }

        Long id = request.getId();
        String studentId = request.getStudentId();
        String contestId = request.getContestId();
        Integer times = request.getTimes();
        Integer time = request.getTime();
        Integer score = request.getScore();
        String sortField = request.getSortField();
        String sortOrder = request.getSortOrder();
//        Date createTime = request.getCreateTime();
//        Date updateTime = request.getUpdateTime();

        queryWrapper.eq(id != null && id > 0,"id", id);
        queryWrapper.like(StringUtils.isNotBlank(studentId), "studentId", "%" + studentId + "%");
        queryWrapper.like(StringUtils.isNotBlank(contestId), "contestId", "%" + contestId + "%");
        queryWrapper.eq(times != null && times > 0,"times", times);
        queryWrapper.eq(time != null &&time > 0,"time", time);
        queryWrapper.eq(score != null && score >= 0,"score", score);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);

        return queryWrapper;
    }
}




